package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.JdUser;
import com.service.UserService;
import com.util.UserUtil;
import com.vo.QueryVo;
import com.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping ("user")
@ResponseBody
public class UserController {
    
    @Resource
    private UserService userService;
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVo<JdUser> queryBySessionUser(HttpServletRequest request){
        Integer userNum = UserUtil.getUserNum(request.getSession());
        if(userNum != null){
            JdUser jdUser = userService.queryByKey(userNum);
            if (jdUser != null){
                return new ResultVo<>(jdUser);
            }
        }
        return new ResultVo<>();
    }
    
    @RequestMapping(value = "quit" , method = RequestMethod.GET)
    public ResultVo<JdUser> quit(HttpServletRequest request){
        UserUtil.closeSession(request.getSession());
        return new ResultVo<>();
    }
    
    
    @RequestMapping (value = "hidden", method = RequestMethod.GET)
    public ResultVo<JdUser> hidden(HttpServletRequest request) {
        
        Integer userNum = UserUtil.getUserNum(request.getSession());
        System.out.println("========================userNum : " + userNum);
        ResultVo<JdUser> resultVo = new ResultVo();
        if (userNum != null) {
            JdUser user = userService.queryByKey(userNum);
            if (user != null) {
                //1为管理员通过
                if (user.getU_logInName().equals("root")) {
                    resultVo.setStatus(1);
                }
            }
        }
        return resultVo;
    }
    
    @RequestMapping (value = "logIn", method = RequestMethod.POST)
    public ResultVo<JdUser> logIn(HttpServletRequest request , JdUser user) {
        System.out.println("user : " + user);
        
        JdUser newUser = null;
        List<JdUser> list = userService.queryByType(user);
        if (!list.isEmpty()) {
            newUser = list.get(0);
        }else{
            user.setU_phone(user.getU_logInName());
            user.setU_logInName(null);
            List<JdUser> list1 = userService.queryByType(user);
            if (!list1.isEmpty()){
                newUser = list1.get(0);
            }
        }
        
        if (newUser != null) {
            
            if (user.getU_logInName() != null){
                if (!newUser.getU_logInName().equals(user.getU_logInName())) {
                    return new ResultVo<>(500 , "输入的账号有误!");
                }
                
                if (newUser.getU_phone()!=null && user.getU_phone() != null){
                    if (!newUser.getU_phone().equals(user.getU_phone())) {
                        return new ResultVo<>(500 , "输入的号码有误!");
                    }
                }
            }
            
            if (!newUser.getU_passWord().equals(user.getU_passWord())) {
                return new ResultVo<>(500 , "账号或密码有误!");
            }
        }else{
            return new ResultVo<>(500,"账号不存在！");
        }
        
        //存储会话账号信息
        if (newUser != null){
            UserUtil.setUserNum(request.getSession() , newUser.getU_num());
        }
        return new ResultVo<>();
    }
    
    @RequestMapping (value = "list", method = RequestMethod.GET)
    public ResultVo<JdUser> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo) {
        PageInfo<JdUser> list = userService.queryByPage(pageNum , pageSize , vo);
        return new ResultVo<>(list);
    }
    
    @RequestMapping (value = "{u_num}", method = RequestMethod.GET)
    public ResultVo<JdUser> queryByKey(@PathVariable ("u_num") Integer u_num) {
        return new ResultVo<>(userService.queryByKey(u_num));
    }
    
    @RequestMapping (value = "", method = RequestMethod.POST)
    public ResultVo<JdUser> add(JdUser user) {
        int add = userService.add(user);
        if (add == 0) {
            return new ResultVo<>(500 , "服务器异常！");
        }
        return new ResultVo<>();
    }
    
    @RequestMapping (value = "{u_num}")
    public ResultVo<JdUser> del(@PathVariable ("u_num") Integer u_num) {
        int del = userService.del(u_num);
        if (del == 0) {
            new ResultVo<>(500 , "服务器异常!");
        }
        return new ResultVo<>();
    }
    
    @RequestMapping (value = "", method = RequestMethod.PUT)
    public ResultVo<JdUser> update(HttpServletRequest request, JdUser user) {
        
        //修改密码
        String newPassword = null;
        String oldPassword = null;
        if (!"".equals(user.getNewPassword()) && user.getNewPassword()!=null){
            newPassword = user.getNewPassword().trim();
        }
        if (!"".equals(user.getU_logInName()) && user.getOldPassword()!=null){
            oldPassword = user.getOldPassword().trim();
        }
    
        user.setU_num(UserUtil.getUserNum(request.getSession()));
        
        if (newPassword != null && oldPassword != null) {
            JdUser t = userService.queryByKey(user.getU_num());
            if (t.getU_passWord().equals(oldPassword)){
                user.setU_passWord(newPassword);
            }else{
                new ResultVo<>(500 , "旧密码不正确！");
            }
        }
        
        int update = userService.update(user);
        if (update == 0) {
            new ResultVo<>(500 , "服务器异常！");
        }
        return new ResultVo<>();
    }
    
}
